package com.bishe.psy.service.impl;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.dao.BookingMapper;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.Booking;
import com.bishe.psy.service.IBookingService;
import com.bishe.psy.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-05-02
 * Time: 9:33
 */

@Slf4j
@Service
public class BookingServiceImpl implements IBookingService {

    @Resource
    private BookingMapper bookingMapper;

    @Override
    public void create(Booking booking) {

        //1.参数校验
        if(booking == null || booking.getPostUserId() == null || booking.getReceiveUserId() == null
        || StringUtil.isEmpty(booking.getBookingTime())) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.设置默认值
        booking.setState((byte) 0);
        booking.setDeleteState((byte) 0);
        Date date = new Date();
        booking.setCreateState(date);
        booking.setUpdateState(date);

        //3.调用数据库
        int row = bookingMapper.insertSelective(booking);
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public Booking selectByPostUserId(Long postUserId, Long receiveUserId) {

        //1.参数校验
        if(postUserId == null || postUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.根据id查询记录
        Booking booking = bookingMapper.selectByPostUserId(postUserId,receiveUserId);

        //3.返回结果
        return booking;

    }

    @Override
    public List<Booking> selectAll(Long postUserId) {

        //1.参数检验
        if(postUserId == null || postUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.调用数据库
        List<Booking> bookings = bookingMapper.selectAll(postUserId);

        //3.返回数据
        return bookings;
    }

    @Override
    public List<Booking> selectAllCon(Long receiveUserId) {
        //1.参数检验
        if(receiveUserId == null || receiveUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.调用数据库
        List<Booking> bookings = bookingMapper.selectAllCon(receiveUserId);

        //3.返回数据
        return bookings;
    }

    @Override
    public List<Booking> selectIsNoBooking(Long receiveUserId) {
        //1.参数检验
        if(receiveUserId == null || receiveUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.调用数据库
        List<Booking> bookings = bookingMapper.selectIsNoBooking(receiveUserId);

        //3.返回数据
        return bookings;
    }

    @Override
    public void updateStateById(Booking booking) {
        //1.参数检验
        if(booking.getId() == null ||  booking == null ) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.调用数据库
        int row = bookingMapper.updateByPrimaryKeySelective(booking);

        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

    }

    @Override
    public Booking selectById(Long id) {

        //1.参数校验
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.查询结果
        Booking booking = bookingMapper.selectByPrimaryKey(id);

        //3.返回结果
        return booking;
    }
}
